package rldungeon.help

import net.cyndeline.scalarlib.rldungeon.grammar.ComponentProduction
import scalax.collection.immutable.Graph
import net.cyndeline.scalarlib.rldungeon.grammar.util.Morphism
import net.cyndeline.scalarlib.rlgraph.util.VertexReplacer

/**
 * The right-hand side in a production that adds an object to a room.
 *
 * @param v Vertex in the pattern (found in the left-hand side of the morphism) to add the object to.
 */
class RoomVertexObjectAugmentation(v: RoomVertex) extends ComponentProduction[RoomVertex, CorridorEdge] {
  private var objectId = 0

  def apply(morphism: Morphism[RoomVertex], totalGraph: Graph[RoomVertex, CorridorEdge]): Graph[RoomVertex, CorridorEdge] = {
    val eFact = new CorridorCopyFactory[RoomVertex]()
    val vertexReplacer = new VertexReplacer[RoomVertex, CorridorEdge](eFact)

    val vertexSoughtFor: RoomVertex = morphism.getVertexCorrespondingTo(v)
    val newVertex = vertexSoughtFor.addObject(objectId)
    objectId += 1

    vertexReplacer.replace(vertexSoughtFor, newVertex, totalGraph)
  }
}
